<script>
import api from '@/common/api.js';
export default {
	globalData: {
		assetTags: [], // 所有图片标签
		categories: [], // 所有分类标签
		platforms: []
	},
	onLaunch: function() {
		console.log('App Launch');
		let userInfo = uni.getStorageSync('userinfo');
		let token = uni.getStorageSync('access_token');
		if (userInfo && token) {
			this.$store.commit('login', userInfo);
			return;
		}
		// this.login();
		// uni.navigateTo({
		// 	url: '../pages/loginWays/loginWays'
		// });
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	},

	methods: {
		// 通过微信登录接口获取code
		getCode() {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					success(res) {
						resolve(res.code);
					},
					fail(err) {
						reject(new Error('微信登录失败'));
					}
				});
			});
		},

		login() {
			// let appid = this.appid;
			// let secret = this.secret;

			// jscode2session 通过code拿到openid
			this.getCode().then(code => {
				// return uni.request({
				// 	url: 'https://api.weixin.qq.com/sns/jscode2session',
				// 	data: {
				// 		appid,
				// 		secret,
				// 		js_code: code,
				// 		grant_type: 'authorization_code'
				// 	}
				// });
				this.$ajax({
					url: api.login,
					data: {
						code
					},
					success: result => {
						console.log('result ==>', result);
						if (result.statusCode == 200) {
							let data = result.data.data;
							if (data.type) {
								console.log('登录成功');
								// uni.setStorageSync('access_token', data.access_token);
								// this.$store.commit('login', data.user);
								uni.navigateTo({
									url: '../pages/loginWays/loginWays'
								});
							} else {
								console.log('登录失败');
								uni.setStorageSync('openid', data.open_id); // openid
								uni.navigateTo({
									url: '../pages/loginWays/loginWays'
								});
							}
						}
					},
					fail: e => {
						console.log('登录失败 ==>', e);
					}
				});
			});
			// .then(res => {
			// 	console.log('res ==>', res);
			// 	let data = res.data;
			// 	uni.setStorageSync('openid', data.openid); // openid
			// 	uni.setStorageSync('session_key', data.session_key); // session_key

			// 	// 拿到openid走登录接口
			// 	uni.request({
			// 		url: this.$baseURL + api.login,
			// 		data: {
			// 			code: data.openid
			// 			// open_id: '123123123123123123123'
			// 		},
			// 		success: result => {
			// 			console.log('result ==>', result);
			// 			let type = result.data.data.type;
			// 			if (type) {
			// 				console.log('登录成功');
			// 				uni.setStorageSync('access_token', result.data.data.access_token);
			// 				// uni.setStorageSync('userinfo', result.data.data.user);
			// 				console.log("this ==>", this);
			// 				this.$store.commit('login', result.data.data.user);
			// 			} else {
			// 				console.log('登录失败');
			// 				uni.navigateTo({
			// 					url: '../login/login'
			// 				});
			// 			}
			// 		},
			// 		fail: e => {
			// 			console.log('登录失败 ==>', e);
			// 		}
			// 	});
			// });
		},
	}
};
</script>

<style lang="scss">
@import '@/uni_modules/uview-ui/index.scss';

/*每个页面公共css */
.tags-popup-btn .u-button {
	width: 90rpx !important;
	height: 60rpx !important;
	border-radius: 10rpx !important;
}

.uni-indexed-list__menu {
	// position: absolute;
	// top: 0;
	// right: 0;
	overflow-y: auto;
}
.uni-indexed-list__menu-item {
	// margin-bottom: 20rpx;
}

/* 上传组件start */
.u-upload__wrap__preview {
	border-radius: 20rpx !important;
}
.u-upload__deletable {
	height: 46rpx !important;
	width: 46rpx !important;
	background-color: #ff002a !important;
}
/* 上传组件end */

image {
	will-change: transform;
}
</style>
